package com.example.board.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.board.mapper.RegionMapper;
import com.example.board.pojo.dto.DeptDto;
import com.example.board.pojo.dto.UserDto;
import com.example.board.pojo.dto.UserRecordDto;
import com.example.board.pojo.vo.*;
import com.example.board.service.RegionService;
import com.example.board.util.DateQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.summingLong;


@Service
@RequiredArgsConstructor
@Slf4j
public class RegionServiceImpl implements RegionService {


    private final RegionMapper regionMapper;


    @Override
    public Map<String, Object> getDistributorGrade(String province, String region) {
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)) {
            province = "广东省";
            region = "深圳市";
        }
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("s", 0.12);
        hashMap.put("a", 0.18);
        hashMap.put("b", 0.3);
        hashMap.put("c", 0.4);
        hashMap.put("areaName", province + "-" + region);
        hashMap.put("total", 200);

        return hashMap;
    }

    @Override
    public AllRegionCity getAllRegionCity() {
        String all = regionMapper.findAllRegionCity();
        //总利润
        ItemsVal itemsVal = new ItemsVal();
        itemsVal.setCompanyGrossProfit("0.8181");
        itemsVal.setCostOfTransportation("12");
        itemsVal.setCostOfMaterial("12");
        itemsVal.setOfflinePromotionCost("91");
        itemsVal.setOlinePromotionCost("20");

        AllRegionCity allRegionCity = new AllRegionCity();
        allRegionCity.setTotal(all);
        allRegionCity.setItemsVal(itemsVal);

        return allRegionCity;
    }


    @Override
    public RegionCity getWeakRegionCity(String province, String region) throws ParseException {
        //本周第一天
        String weekStart = DateQuery.getWeekStart();
//        String weekStart = "2022-01-10 00:00:00";

        //1.具有转换功能的对象
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //3.LocalDate发动，将字符串转换成  df格式的LocalDateTime对象，的功能
        LocalDateTime LocalTime = LocalDateTime.parse(weekStart, df);

        //查询每周数量 拜访 回访
        List<NumberAmount> cityOrderRate = regionMapper.findVistWeakRate(province, region, LocalTime);
        List<NumberAmount> cityOrderRate1 = regionMapper.findVistWeakRate1(province, region, LocalTime);

        List<NumberAmount> cityOrderRatess = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(cityOrderRate1) && CollectionUtils.isNotEmpty(cityOrderRate)) {
            List<NumberAmount> cityOrderRates = new ArrayList<>();
            cityOrderRates.addAll(cityOrderRate1);
            cityOrderRates.addAll(cityOrderRate);
            Map<String, Long> collect = cityOrderRates.stream().collect(Collectors.groupingBy(NumberAmount::getName, summingLong(NumberAmount::getData)));

            for (Map.Entry<String, Long> stringLongEntry : collect.entrySet()) {
                NumberAmount numberAmount = new NumberAmount();
                numberAmount.setData(stringLongEntry.getValue());
                numberAmount.setName(stringLongEntry.getKey());
                cityOrderRatess.add(numberAmount);
            }
        }
        if (CollectionUtils.isNotEmpty(cityOrderRate1) && CollectionUtils.isEmpty(cityOrderRate)) {
            Map<String, Long> collect = cityOrderRate1.stream().collect(Collectors.groupingBy(NumberAmount::getName, summingLong(NumberAmount::getData)));
            for (Map.Entry<String, Long> longEntry : collect.entrySet()) {
                NumberAmount numberAmount = new NumberAmount();
                numberAmount.setData(longEntry.getValue());
                numberAmount.setName(longEntry.getKey());
                cityOrderRatess.add(numberAmount);
            }
        }

        if (CollectionUtils.isEmpty(cityOrderRate1) && CollectionUtils.isNotEmpty(cityOrderRate)) {
            Map<String, Long> collect2 = cityOrderRate.stream().collect(Collectors.groupingBy(NumberAmount::getName, summingLong(NumberAmount::getData)));
            for (Map.Entry<String, Long> longEntry : collect2.entrySet()) {
                NumberAmount numberAmount = new NumberAmount();
                numberAmount.setData(longEntry.getValue());
                numberAmount.setName(longEntry.getKey());
                cityOrderRatess.add(numberAmount);
            }
        }

        Date[] weekDay = DateQuery.getWeekDay();

        List<NumberAmount> experienceDayResList = new ArrayList<>(cityOrderRatess);
        for (Date date : weekDay) {
            // 当前日期不存在则新增
            String dateStr = DateUtil.format(date, "dd");
            // 进行匹配
            if (!experienceDayResList.stream().filter(item -> dateStr.equals(item.getName())).findAny().isPresent()) {
                NumberAmount experienceDayRes = NumberAmount.builder()
                        .name(dateStr)
                        .data(0L)
                        .build();
                experienceDayResList.add(experienceDayRes);
            }
        }

        List<NumberAmount> collect = experienceDayResList.stream().sorted(Comparator.comparing(NumberAmount::getName)).collect(Collectors.toList());

        for (int i = 0; i < collect.size(); i++) {
            collect.get(i).setName("星期" + ((i + 1) % 7) + "");
        }
        List<String> week = DateQuery.week(collect.stream().map(NumberAmount::getName).collect(Collectors.toList()));
        for (int i = 0; i < week.size(); i++) {
            collect.get(i).setName(week.get(i));
        }


        //已完成
        Long sum = collect.stream().mapToLong(NumberAmount::getData).sum();
        RegionCity regionCity = new RegionCity();
        regionCity.setChartData(collect);
        regionCity.setTotal(10000L);
        regionCity.setWait(((regionCity.getTotal()) - sum));
        regionCity.setFinished(BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(regionCity.getTotal()), 2, RoundingMode.HALF_UP));

        return regionCity;
    }

    @Override
    public RegionCity getWeakOrderCity(String province, String region) throws ParseException {

        //本周第一天
        String weekStart = DateQuery.getWeekStart();
//        String weekStart = "2022-10-01 00:00:00";

        //1.具有转换功能的对象
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //3.LocalDate发动，将字符串转换成  df格式的LocalDateTime对象，的功能
        LocalDateTime LocalTime = LocalDateTime.parse(weekStart, df);

        //查询每周数量
        List<NumberAmount> cityOrderRate = regionMapper.findOrderWeakRate(province, region, LocalTime);

        Date[] weekDay = DateQuery.getWeekDay();

        List<NumberAmount> experienceDayResList = new ArrayList<>(cityOrderRate);
        for (Date date : weekDay) {
            // 当前日期不存在则新增
            String dateStr = DateUtil.format(date, "dd");
            // 进行匹配
            if (!experienceDayResList.stream().filter(item -> dateStr.equals(item.getName())).findAny().isPresent()) {
                NumberAmount experienceDayRes = NumberAmount.builder()
                        .name(dateStr)
                        .data(0L)
                        .build();
                experienceDayResList.add(experienceDayRes);
            }
        }

        List<NumberAmount> collect = experienceDayResList.stream().sorted(Comparator.comparing(NumberAmount::getName)).collect(Collectors.toList());

        for (int i = 0; i < collect.size(); i++) {
            collect.get(i).setName("星期" + ((i + 1) % 7) + "");

        }
        List<String> week = DateQuery.week(collect.stream().map(NumberAmount::getName).collect(Collectors.toList()));
        for (int i = 0; i < week.size(); i++) {
            collect.get(i).setName(week.get(i));
        }


        //已完成
        Long sum = collect.stream().mapToLong(NumberAmount::getData).sum();
        RegionCity regionCity = new RegionCity();
        regionCity.setChartData(collect);
        regionCity.setTotal(10000L);
        regionCity.setWait((regionCity.getTotal()) - sum );
        regionCity.setFinished(BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(regionCity.getTotal()), 2, RoundingMode.HALF_UP));


        return regionCity;
    }


    @Override
    public Map<String, Object> getConversionRate(String province, String region, String startData) {

        //1.具有转换功能的对象
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //3.LocalDate发动，将字符串转换成  df格式的LocalDateTime对象，的功能
        LocalDateTime startLocalTime = null;
        LocalDateTime endLocalTime = null;
        if (StringUtils.isEmpty(startData)) {
            startLocalTime = LocalDateTime.parse(DateQuery.getMothStart(), df);
            endLocalTime = LocalDateTime.now();
        } else {
            startLocalTime = LocalDateTime.parse(DateQuery.getMothSomeStart(startData) + " 00:00:00", df);
            endLocalTime = LocalDateTime.parse(DateQuery.getMothSomeEnd(startData) + " 23:00:00", df);
        }
        List<String> cityList = new ArrayList<>();
        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)) {
            province = "广东省";
            //查询城市下所有区域
            cityList = regionMapper.findCity(province);
        }
        if (StringUtils.isNotEmpty(province) && StringUtils.isEmpty(region)) {
            //查询城市下所有区域
            province = province;
            cityList = regionMapper.findCity(province);
        }
        if (StringUtils.isNotEmpty(province) && StringUtils.isNotEmpty(region)) {
            province = province;
            cityList.add(region);
        }
        if (CollectionUtils.isNotEmpty(cityList)){
            //查询拜访用户量 size
            List<UserDto> vo = regionMapper.findConversion(province, cityList, startLocalTime, endLocalTime);
            List<UserDto> vo1 = regionMapper.findUserRe(province, cityList, startLocalTime, endLocalTime);
            //查询这些用户下单的人
            List<UserDto> vos = regionMapper.findOrders(province, cityList, startLocalTime, endLocalTime);

            List<Map<String, Object>> chartData = new ArrayList<>();

            int viTotal = 0;
            int orTotal = 0;

            for (String city : cityList) {

                if (CollectionUtils.isNotEmpty(vo)){
                    for (UserDto userDto : vo) {
                        if (city.equals(userDto.getCity())){
                            viTotal =+ userDto.getDate();
                        }
                    }
                }
                if (CollectionUtils.isNotEmpty(vo1)){
                    for (UserDto userDto : vo1) {
                        if (city.equals(userDto.getCity())){
                            viTotal =+ userDto.getDate();
                        }
                    }
                }
                if (CollectionUtils.isNotEmpty(vos)){
                    for (UserDto userDto : vos) {
                        if (city.equals(userDto.getCity())){
                            orTotal =+ userDto.getDate();
                        }
                    }
                }
                Map<String, Object> obj = new HashMap<>();
                obj.put("name", city);
                obj.put("data1",new BigDecimal(0));
                //转化率： 下单量/拜访量
                if (viTotal != 0){
                    obj.put("data1", new BigDecimal(orTotal).divide(new BigDecimal(viTotal), 2, RoundingMode.HALF_UP));
                }
                obj.put("data2", viTotal);
//            obj.put("data3", orTotal);
                chartData.add(obj);

                viTotal = 0;
                orTotal = 0;
            }

            List<Map<String, Object>> data1 = chartData.stream().sorted(Comparator.comparing((Map<String, Object> chart) -> (BigDecimal) chart.get("data1")).reversed()).limit(4).collect(Collectors.toList());

            Map<String, Object> chartDataCitys = new HashMap<>();
            chartDataCitys.put("chartData", data1);
            return chartDataCitys;
        }else {
            List<Map<String, Object>> chartData = new ArrayList<>();
            Map<String, Object> obj = new HashMap<>();
            if (StringUtils.isNotEmpty(province) && StringUtils.isNotEmpty(region)){
                obj.put("name", region);
            }else {
                obj.put("name", province);
            }

            obj.put("data1",new BigDecimal(0));
            //转化率： 下单量/拜访量
            obj.put("data1", new BigDecimal(0));
            obj.put("data2", 0);
//            obj.put("data3", orTotal);
            chartData.add(obj);
            Map<String, Object> chartDataCitys = new HashMap<>();
            chartDataCitys.put("chartData", chartData);
            return chartDataCitys;
        }


    }

    @Override
    public Map<String, Object> getRegionRank(String province, String region) {

        List<String> cityList = new ArrayList<>();

        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)) {
            province = "广东省";
            //查询城市下所有区域
            cityList = regionMapper.findCityOrder(province);
        }

        if (StringUtils.isNotEmpty(province) && StringUtils.isEmpty(region)) {
//            province=province;
            //查询城市下所有区域
            cityList = regionMapper.findCityOrder(province);
        }
        if (StringUtils.isNotEmpty(province) && StringUtils.isNotEmpty(region)) {
            province = province;
            cityList.add(region);
        }

        //1.具有转换功能的对象
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //3.LocalDate发动，将字符串转换成  df格式的LocalDateTime对象，的功能
        LocalDateTime startLocalTime = LocalDateTime.parse(DateQuery.getMothStart(), df);

        Map<String, Object> chartDataCitys = new HashMap<>();
        List<Map<String, Object>> chartData = new ArrayList<>();
        chartDataCitys.put("area", province);

        //最大地区总金额
        UserDto regionRank = regionMapper.findRegionRank1(province, startLocalTime);

        //地区金额
        List<UserDto> cityOrderMoths = regionMapper.findRegionRank(province, cityList, startLocalTime);

        for (String city : cityList) {

            Map<String, Object> obj = new HashMap<>();
            obj.put("name", city);
            obj.put("data", new BigDecimal(0));

            if (CollectionUtils.isNotEmpty(cityOrderMoths)){
                for (UserDto cityOrderMoth : cityOrderMoths) {
                    if (regionRank.getDate()!=0 && city.equals(cityOrderMoth.getCity())){
//                        System.out.println(city+ " 金额： "+cityOrderMoth.getDate() +" : "+regionRank.getDate());
//                        System.out.println(city+" 率： "+new BigDecimal(cityOrderMoth.getDate()).divide(new BigDecimal(regionRank.getDate()), 2, RoundingMode.HALF_UP));

                        obj.put("data", new BigDecimal(cityOrderMoth.getDate()).divide(new BigDecimal(regionRank.getDate()), 2, RoundingMode.HALF_UP));
                    }
                }

            }
            chartData.add(obj);
        }


        List<Map<String, Object>> data1 = chartData.stream().sorted(Comparator.comparing((Map<String, Object> chart) -> (BigDecimal) chart.get("data")).reversed()).limit(5).collect(Collectors.toList());


        chartDataCitys.put("chartData", data1);

        return chartDataCitys;
    }

    @Override
    public List<ReCustomerVo> getReCustomerIOrder(String province, String region) {
        //已购未返单
        List<UserRecordDto> reCustomerVoList = regionMapper.getReCustomerIOrder(province, region);
        List<UserRecordDto> collect = reCustomerVoList.stream().filter(x -> x.getTotal() == 1).collect(Collectors.toList());
        List<ReCustomerVo> customerVoList = new ArrayList<>();

        for (UserRecordDto reCustomerVo : collect) {
            ReCustomerVo reCustomerVos = new ReCustomerVo();

            reCustomerVos.setBelong(reCustomerVo.getProvince() + "-" + reCustomerVo.getCity());
            reCustomerVos.setStatus("1");
            reCustomerVos.setCustomerType("0");
            reCustomerVos.setImg(reCustomerVo.getImg());
            reCustomerVos.setGoodsNumber(reCustomerVo.getGoodsNumber());
            reCustomerVos.setSalesman(reCustomerVo.getUserName());
            reCustomerVos.setName(reCustomerVo.getShopName());
            reCustomerVos.setLastOrderTime(reCustomerVo.getLastOrderTime());
            reCustomerVos.setIdNumber(reCustomerVo.getIdNumber());

            customerVoList.add(reCustomerVos);
        }

        return customerVoList;
    }

    @Override
    public List<ReCustomerVo> getReCustomerTime(String province, String region) {
        List<UserRecordDto> reCustomerVoList = regionMapper.findReCustomerTime(province, region);

        //1.具有转换功能的对象
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        int time = 0;
        int total = 0;
        //3.LocalDate发动，将字符串转换成  df格式的LocalDateTime对象，的功能
        for (int i = 0; i < reCustomerVoList.size() - 1; i++) {
            if (i % 2 == 0) {
                if (reCustomerVoList.get(i + 1).getSeqnum() - reCustomerVoList.get(i).getSeqnum() == 1) {
                    time += DateQuery.countDay(reCustomerVoList.get(i).getLastOrderTime()+"",reCustomerVoList.get(i+1).getLastOrderTime()+"");
                    total++;

                }
            }

        }



        List<ReCustomerVo> customerVoList = new ArrayList<>();
        if (total != 0) {
            //平均返单时间
            int times = time / total;
            //查询每个用户最大的返单时间
            List<UserRecordDto> reCustomerIOrder = regionMapper.getReCustomerIOrder(province, region);
            List<UserRecordDto>  reCustomerVoLists = reCustomerIOrder.stream().filter(x -> LocalDateTime.now().minusDays(times).isAfter(x.getLastOrderTime())).collect(Collectors.toList());
            for (UserRecordDto reCustomerVo : reCustomerVoLists) {
                ReCustomerVo reCustomerVos = new ReCustomerVo();

                reCustomerVos.setBelong(reCustomerVo.getProvince() + "-" + reCustomerVo.getCity());
                reCustomerVos.setStatus("0");
                reCustomerVos.setCustomerType("0");
                reCustomerVos.setImg(reCustomerVo.getImg());
                reCustomerVos.setGoodsNumber(reCustomerVo.getGoodsNumber());
                reCustomerVos.setSalesman(reCustomerVo.getUserName());
                reCustomerVos.setName(reCustomerVo.getShopName());
                reCustomerVos.setLastOrderTime(reCustomerVo.getLastOrderTime());
                reCustomerVos.setIdNumber(reCustomerVo.getIdNumber());

                customerVoList.add(reCustomerVos);
            }
            System.out.println("times: "+times);
        }



        return customerVoList;
    }


    @Override
    public List<AreaAchievementVo> getAreaAchievementCity(String province, String region, String teamName, String startTime, String status) {
        if (StringUtils.isEmpty(status)) {
            status = "1";
        }

        if (status.equals("0")) {
            List<AreaAchievementVo> areaAchievementVos = new ArrayList<>();
            return areaAchievementVos;
        }
        LocalDate someStart = null;
        LocalDate someEnd = null;
        LocalDate someStartLast = null;
        LocalDate someEndLast = null;
        if (StringUtils.isEmpty(startTime)) {
            String mothNow = (DateTime.now() + "").split(" ")[0].split("-")[0] + (DateTime.now() + "").split(" ")[0].split("-")[1];
            String mothLast = (DateTime.now() + "").split(" ")[0].split("-")[0] + (Integer.parseInt((DateTime.now() + "").split(" ")[0].split("-")[1]) - 1);
            //这月的
            someStart = DateQuery.getMothSomeStart(mothNow);
            someEnd = DateQuery.getMothSomeEnd(mothNow);
            //上月的
            someStartLast = DateQuery.getMothSomeStart(mothLast);
            someEndLast = DateQuery.getMothSomeEnd(mothLast);

        }

        if (StringUtils.isNotEmpty(startTime) && !startTime.substring(4).equals("01")) {
            someStart = DateQuery.getMothSomeStart(startTime);
            someEnd = DateQuery.getMothSomeEnd(startTime);

            //上月的
            someStartLast = DateQuery.getMothSomeStart((Integer.parseInt(startTime) - 1) + "");
            someEndLast = DateQuery.getMothSomeEnd((Integer.parseInt(startTime) - 1) + "");
        }
        if (StringUtils.isNotEmpty(startTime) && startTime.substring(4).equals("01")) {
            someStart = DateQuery.getMothSomeStart(startTime);
            someEnd = DateQuery.getMothSomeEnd(startTime);

            //上年的最后一个月的
            someStartLast = DateQuery.getMothSomeStart((Integer.parseInt(startTime) - 89) + "");
            someEndLast = DateQuery.getMothSomeEnd((Integer.parseInt(startTime) - 89) + "");
        }

        List<String> cityList = new ArrayList<>();

        if (StringUtils.isNotEmpty(province) && StringUtils.isEmpty(region)) {
            province = province;
            //查询城市下所有区域
            cityList = regionMapper.findAllCity(province);
        }

        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(region)) {
            province = "广东省";
            //查询城市下所有区域
            cityList = regionMapper.findAllCity(province);
        }

        if (StringUtils.isNotEmpty(province) && StringUtils.isNotEmpty(region)) {
            province = province;
            cityList.add(region);
        }

        //1.具有转换功能的对象
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //3.LocalDate发动，将字符串转换成  df格式的LocalDateTime对象，的功能 传过来那月
        LocalDateTime startLocalTime = LocalDateTime.parse(someStart + " 00:00:00", df);
        LocalDateTime endLocalTime = LocalDateTime.parse(someEnd + " 00:00:00", df);

        //传过来那月 的上月
        LocalDateTime startLocalTimeLast = LocalDateTime.parse(someStartLast + " 00:00:00", df);
        LocalDateTime endLocalTimeLast = LocalDateTime.parse(someEndLast + " 00:00:00", df);

       if (CollectionUtils.isNotEmpty(cityList)){
           //查进销商数量
           List<DeptDto> totalDistributorNumber = regionMapper.findDistributorNumber(province, cityList, endLocalTime,teamName);

           //查询地区下进销商数 订单数 和总产出  startLocalTime.minusDays(times), startLocalTime,
           List<DeptDto> vo = regionMapper.findDistributors(province, cityList,
                   status, startLocalTime, endLocalTime, teamName);


           //客户增长率 本期周期统计总数-对比周期总数÷对比周期总数×100%
           //上一个月的 客户 2
           System.out.println(" 客户增长");
           List<DeptDto> areaAchievementDto = regionMapper.findURMoth(province, cityList, status, startLocalTimeLast, endLocalTimeLast, teamName);

           //这个月 s 0
           List<DeptDto> areaAchievementDtos = regionMapper.findURMoth(province, cityList, status, startLocalTime, endLocalTime, teamName);

           //订单增长 上一个月 22
           System.out.println(" 订单增长");
           List<DeptDto> beforeOrderDto = regionMapper.findOrderMoth(province, cityList, status, startLocalTimeLast, endLocalTimeLast, teamName);

           //订单增长 这个月 11
           List<DeptDto> nowOrderDto = regionMapper.findOrderMoth(province, cityList, status, startLocalTime, endLocalTime, teamName);

           List<AreaAchievementVo> areaAchievementVos = new ArrayList<>();
           //上月
           int totalLast = 0;
           int totalMoth = 0;

           //凑数据
           for (String city : cityList) {
               AreaAchievementVo areaAchievementVo = new AreaAchievementVo();

               areaAchievementVo.setAreaName(province + "-" + city);
               areaAchievementVo.setStatus(1);
               areaAchievementVo.setDistributorNumber(0);
               areaAchievementVo.setCustomerInc(new BigDecimal(0));
               areaAchievementVo.setOrderNumber(0);
               areaAchievementVo.setTotalProduce(0.00);
               areaAchievementVo.setOrderNumberInc(new BigDecimal(0));

               if (StringUtils.isNotEmpty(teamName)){
                   areaAchievementVo.setAreaName(province + "-" + city+"-"+teamName);
               }
               //进销商数量
               if (CollectionUtils.isNotEmpty(totalDistributorNumber)){
                   for (DeptDto deptDto : totalDistributorNumber) {
                       if (deptDto.getCity().equals(city)){
                           areaAchievementVo.setDistributorNumber(deptDto.getTotal());
                       }
                   }
               }

               //客户增长
               if (CollectionUtils.isNotEmpty(areaAchievementDto)){
                   for (DeptDto deptDto : areaAchievementDto) {
                       if (deptDto.getCity().equals(city)){
                           totalLast = deptDto.getTotal();
                       }
                   }
               }

               if (CollectionUtils.isNotEmpty(areaAchievementDtos)){
                   for (DeptDto deptDto : areaAchievementDtos) {
                       if (deptDto.getCity().equals(city)){
                           totalMoth = deptDto.getTotal();
                       }
                   }
               }
               //客户增长率
               if (totalLast!=0){
                   areaAchievementVo.setCustomerInc((new BigDecimal(totalMoth).subtract(new BigDecimal(totalLast))).divide(new BigDecimal(totalLast),2, RoundingMode.HALF_UP));
               }
               //订单数量
               if(CollectionUtils.isNotEmpty(vo)){
                   for (DeptDto deptDto : vo) {
                       if (deptDto.getCity().equals(city)){
                           areaAchievementVo.setOrderNumber(deptDto.getTotalOrder());
                           areaAchievementVo.setTotalProduce(deptDto.getTransactionAmount());
                       }
                   }
               }

               totalLast = 0;
               totalMoth = 0;
               //订单增长
               if (CollectionUtils.isNotEmpty(beforeOrderDto)){
                   for (DeptDto deptDto : beforeOrderDto) {
                       if (deptDto.getCity().equals(city)){
                           totalLast = deptDto.getTotal();
                       }
                   }
               }

               if (CollectionUtils.isNotEmpty(nowOrderDto)){
                   for (DeptDto deptDto : nowOrderDto) {
                       if (deptDto.getCity().equals(city)){
                           totalMoth = deptDto.getTotal();
                       }
                   }
               }
               //订单增长率
               if (totalLast!=0){
                   areaAchievementVo.setOrderNumberInc((new BigDecimal(totalMoth).subtract(new BigDecimal(totalLast))).divide(new BigDecimal(totalLast),2, RoundingMode.HALF_UP));
               }

               totalLast = 0;
               totalMoth = 0;

               areaAchievementVos.add(areaAchievementVo);
           }

           //排序总金额
           return areaAchievementVos.stream().sorted(Comparator.comparingDouble(AreaAchievementVo::getTotalProduce).reversed()).collect(Collectors.toList());
       }else {
           List<AreaAchievementVo> areaAchievementVos = new ArrayList<>();
           AreaAchievementVo areaAchievementVo = new AreaAchievementVo();

           if (StringUtils.isNotEmpty(province) && StringUtils.isNotEmpty(region)){
               areaAchievementVo.setAreaName(province + "-" + region);
           }else {
               areaAchievementVo.setAreaName(province);
           }


           areaAchievementVo.setStatus(1);
           areaAchievementVo.setDistributorNumber(0);
           areaAchievementVo.setCustomerInc(new BigDecimal(0));
           areaAchievementVo.setOrderNumber(0);
           areaAchievementVo.setTotalProduce(0.00);
           areaAchievementVo.setOrderNumberInc(new BigDecimal(0));
           areaAchievementVos.add(areaAchievementVo);
           return areaAchievementVos;
       }

    }
}
